package com.softserveinc.data.dal;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.softserveinc.data.help.DBDataProcessor;
import com.softserveinc.data.help.MovieDBOpenHelper;
import com.softserveinc.data.help.MovieDBOpenHelper.MovieDetailsTableMeta;
import com.softserveinc.data.help.MovieDBOpenHelper.MovieImagesTableMeta;
import com.softserveinc.model.Movie;
import com.softserveinc.model.MovieDetails;

public class MovieDetailsDao implements IMovieDetailsDao {

	private static final String TAG = MovieDetailsDao.class.getSimpleName();
	private final MovieDBOpenHelper helper;
	private final SQLiteDatabase database;

	private final DBDataProcessor dataProcessor;

	public MovieDetailsDao(final Context context) {
		helper = new MovieDBOpenHelper(context);
		database = helper.getWritableDatabase();

		dataProcessor = new DBDataProcessor();
	}

	@Override
	public MovieDetails getMovieDetails(final Movie movie) {
		String tableName = MovieDetailsTableMeta.TABLE_NAME;
		String movieIDColumn = MovieDetailsTableMeta.COLUMN_NAME_MOVIE_TMBD_ID;

		String sql = "SELECT * FROM " + tableName + " WHERE " + movieIDColumn
				+ "=?";

		Cursor cursor = database.rawQuery(sql,
				new String[] { "" + movie.getId() });

		MovieDetails movieDetails = dataProcessor
				.parseMovieDetailsCursor(cursor);

		if (movieDetails != null) {
			movieDetails.setMovie(movie);

			addImages(movieDetails);
		}

		return movieDetails;
	}

	private void addImages(final MovieDetails movieDetails) {

		Log.d(TAG, " getImagesForMovie");

		String tableName = MovieImagesTableMeta.TABLE_NAME;
		String movieIDColumn = MovieImagesTableMeta.COLUMN_NAME_MOVIE_TMDB_ID;

		String sql = "SELECT * FROM " + tableName + " WHERE " + movieIDColumn
				+ "=?";

		Cursor cursor = database.rawQuery(sql, new String[] { ""
				+ movieDetails.getMovie().getId() });

		if (!cursor.moveToFirst()) {
			Log.d(TAG, " getImagesForMovie has serious problem");
		}

		dataProcessor.addImagesToMovieDetails(cursor, movieDetails);
	}

	@Override
	public void insertOrUpdate(final MovieDetails movieDetails) {
		if (detailsAvailable(movieDetails)) {
			update(movieDetails);
		} else {
			insert(movieDetails);
		}
	}

	@Override
	public void updateThumbImage(final Movie movie, final String httpUrl,
			final String httpUrlBig, final String fileName) {
		ContentValues values = new ContentValues();

		values.put(MovieImagesTableMeta.COLUMN_NAME_MOVIE_TMDB_ID,
				movie.getId());
		values.put(MovieImagesTableMeta.COLUMN_NAME_THUMB_HTTP, httpUrl);
		values.put(MovieImagesTableMeta.COLUMN_NAME_THUMB_HTTP_BIG, httpUrlBig);
		values.put(MovieImagesTableMeta.COLUMN_NAME_THUMB_LOCAL, fileName);

		String tableName = MovieImagesTableMeta.TABLE_NAME;
		String httpColumn = MovieImagesTableMeta.COLUMN_NAME_THUMB_HTTP;

		database.update(tableName, values, httpColumn + "=?",
				new String[] { httpUrl });
	}

	private void update(final MovieDetails movieDetails) {
		ContentValues detailsValues = createDetailsValues(movieDetails);

		String tableName = MovieDetailsTableMeta.TABLE_NAME;
		String movieIDColumn = MovieDetailsTableMeta.COLUMN_NAME_MOVIE_TMBD_ID;

		int movieID = movieDetails.getMovie().getId();

		database.update(tableName, detailsValues,
				movieIDColumn + "=" + movieID, null);

		Log.d(TAG, "updating images");
		if (movieDetails.getThumbHttpUrls() != null) {
			Log.d(TAG, "updating images in progress");
			tableName = MovieImagesTableMeta.TABLE_NAME;

			String httpColumn = MovieImagesTableMeta.COLUMN_NAME_THUMB_HTTP;

			for (int i = 0; i < movieDetails.getThumbHttpUrls().size(); i++) {
				ContentValues imageValues = createDetailsThumbValues(
						movieDetails, i);

				String httpUrl = movieDetails.getThumbHttpUrls().get(i);
				database.update(tableName, imageValues, httpColumn + "=?",
						new String[] { httpUrl });
			}
		}
	}

	private void insert(final MovieDetails movieDetails) {
		ContentValues detailsValues = createDetailsValues(movieDetails);

		database.insert(MovieDetailsTableMeta.TABLE_NAME, null, detailsValues);

		Log.d(TAG, "updating images");
		if (movieDetails.getThumbHttpUrls() != null) {
			Log.d(TAG, "updating images in progress");
			for (int i = 0; i < movieDetails.getThumbHttpUrls().size(); i++) {
				ContentValues imageValues = createDetailsThumbValues(
						movieDetails, i);

				database.insert(MovieImagesTableMeta.TABLE_NAME, null,
						imageValues);
			}
		}
	}

	@Override
	public void delete(final MovieDetails movieDetails) {
		// TODO Auto-generated method stub

	}

	private ContentValues createDetailsValues(final MovieDetails movieDetails) {
		ContentValues detailsValues = new ContentValues();

		detailsValues.put(MovieDetailsTableMeta.COLUMN_NAME_MOVIE_TMBD_ID,
				movieDetails.getMovie().getId());
		detailsValues.put(MovieDetailsTableMeta.COLUMN_NAME_CAST,
				movieDetails.getCast());
		detailsValues.put(MovieDetailsTableMeta.COLUMN_NAME_OVERVIEW,
				movieDetails.getOverview());
		detailsValues.put(MovieDetailsTableMeta.COLUMN_NAME_MOVIE_TRAILER_URL,
				movieDetails.getTrailerUrl());

		return detailsValues;
	}

	private ContentValues createDetailsThumbValues(
			final MovieDetails movieDetails, final int imageIndex) {

		ContentValues imageValues = new ContentValues();

		imageValues.put(MovieImagesTableMeta.COLUMN_NAME_MOVIE_TMDB_ID,
				movieDetails.getMovie().getId());
		imageValues.put(MovieImagesTableMeta.COLUMN_NAME_THUMB_HTTP,
				movieDetails.getThumbHttpUrls().get(imageIndex));
		imageValues.put(MovieImagesTableMeta.COLUMN_NAME_THUMB_HTTP_BIG,
				movieDetails.getThumbHttpUrlsBig().get(imageIndex));

		if (movieDetails.getThumbLocalUrls() != null) {
			imageValues.put(MovieImagesTableMeta.COLUMN_NAME_THUMB_HTTP_BIG,
					movieDetails.getThumbLocalUrls().get(imageIndex));
		}

		return imageValues;
	}

	private boolean detailsAvailable(final MovieDetails movieDetails) {
		String tableName = MovieDetailsTableMeta.TABLE_NAME;

		String movieIDColumn = MovieDetailsTableMeta.COLUMN_NAME_MOVIE_TMBD_ID;

		String sql = "SELECT * FROM " + tableName + " WHERE " + movieIDColumn
				+ "=?";

		int movieID = movieDetails.getMovie().getId();

		Cursor cursor = database.rawQuery(sql, new String[] { "" + movieID });

		boolean condition = cursor.moveToFirst();

		cursor.close();

		return condition;
	}
}
